import csv
import matplotlib.pyplot as plt
from datetime import  datetime

filename="death_valley_2021_simple.csv"

with open(filename) as f:
    reader=csv.reader(f)
    header_row=next(reader)

    for index,column_header in enumerate(header_row):#此处调用enumerate函数，可以返回每个元素的索引及其值。
        print(index,column_header)
    dates,highs,lows=[],[],[]
    for row in reader:
        current_date=datetime.strptime(row[2],"%Y-%m-%d")
        try:
            high = int(row[3])#程序运行后在此处报错，有可能是存在空值无法转换，所以要进行捕捉异常。防止因为一个缺失值导致文件中断。
            low=int(row[4])
        except ValueError:
            print(f"Missing data for {current_date}")
        else:
            highs.append(high)
            dates.append(current_date)
            lows.append(low)

plt.style.use("classic")
fig,ax=plt.subplots()
ax.plot(dates,highs,color="red",alpha=0.5)
ax.plot(dates,lows,color="blue",alpha=0.5)
ax.fill_between(dates,highs,lows,facecolor="orange")
ax.set_title("Daily High and Low Temperatures,July ",fontsize=24)
ax.set_xlabel("",fontsize=16)
fig.autofmt_xdate()
ax.set_ylabel("Temperature",fontsize=16)
ax.tick_params(labelsize=16)

plt.show()